﻿package com.content.model.LoadedAssets
{

	import com.content.view.Component.CButton.Level1VO;
	
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.puremvc.as3.utilities.startupmanager.interfaces.IStartupProxy;
	
	public class ConfigProxy extends EntityProxy implements IStartupProxy
	{
		public static const NAME:String 						= "ConfigProxy";
        public static const SRNAME:String 						= "ConfigSRProxy";
		public static const LOAD_COMPLETE:String				= NAME + "-" + "LoadComplete";
		public static const LOAD_ERROR:String					= NAME + "-" + "LoadError";
		
		private var _textData:Object;

		public function ConfigProxy() 
		{
			super( NAME, null);
			_textData = new Object();
		}
		
		public function load() :void 
		{
//            sendNotification( AppFacade.CONFIG_LOADING );
			
			var request:URLRequest = new URLRequest("assets/xml/Config.xml");
			var loader:URLLoader = new URLLoader();
			
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			
			loader.load(request);
        }
		
		public var CBtnVOList:Array;
		
		public var Author:String;
		public var Company:String;
		public var Debug:Boolean;
		public var ImgFolder:String;
		public var UserID:String;
		public var Feed_URL:String;

		public var UI_CBarL1_Width:int;
		public var UI_CBarL2_Width:int;
		public var UI_CBarL1_MarginLeft:int;
		public var UI_CBarL2Btn_MarginH:int;
		public var UI_CBarL2Btn_MarginV:int;
		public var UI_CBarL2Btn_Height:int;
		
		public var UI_MainPanel_MarginTop:int;
		public var UI_MainPanel_MarginBottom:int;
		public var UI_MainPanel_MarginLeft:int;
		public var UI_MainPanel_MarginRight:int;
		
		public var UI_InfoTape_MarginTop:int;
		public var UI_InfoTape_Height:int;
		public var UI_InfoTape_MarginLeft:int;
		public var UI_InfoTape_MarginRight:int;
		
		private function loaderCompleteHandler(event:Event):void 
		{	
			// App Vars *************************
			var data:XML = new XML( event.target.data );	
			Author = data.appvar.string.(@ID == "Author");
			Company = data.appvar.string.(@ID == "Company");
			Debug = data.appvar.string.(@ID == "Debug").toLowerCase() == "true"?true:false;
			ImgFolder = data.appvar.string.(@ID == "ImgFolder");
			UserID = data.appvar.string.(@ID == "UserID");
			Feed_URL = data.appvar.string.(@ID == "Feed_URL");
			
			// UI Layout *************************
			UI_CBarL1_Width = int(data.appvar.string.(@ID == "UI_CBarL1_Width"));
			UI_CBarL2_Width = int(data.appvar.string.(@ID == "UI_CBarL2_Width"));
			UI_CBarL1_MarginLeft = int(data.appvar.string.(@ID == "UI_CBarL1_MarginLeft"));
			UI_CBarL2Btn_MarginH = int(data.appvar.string.(@ID == "UI_CBarL2Btn_MarginH"));
			UI_CBarL2Btn_MarginV = int(data.appvar.string.(@ID == "UI_CBarL2Btn_MarginV"));
			UI_CBarL2Btn_Height = int(data.appvar.string.(@ID == "UI_CBarL2Btn_Height"));
			UI_MainPanel_MarginTop = int(data.appvar.string.(@ID == "UI_MainPanel_MarginTop"));
			UI_MainPanel_MarginBottom = int(data.appvar.string.(@ID == "UI_MainPanel_MarginBottom"));
			UI_MainPanel_MarginLeft = int(data.appvar.string.(@ID == "UI_MainPanel_MarginLeft"));
			UI_MainPanel_MarginRight = int(data.appvar.string.(@ID == "UI_MainPanel_MarginRight"));
			UI_InfoTape_MarginTop = int(data.appvar.string.(@ID == "UI_InfoTape_MarginTop"));
			UI_InfoTape_Height = int(data.appvar.string.(@ID == "UI_InfoTape_Height"));
			UI_InfoTape_MarginLeft = int(data.appvar.string.(@ID == "UI_InfoTape_MarginLeft"));
			UI_InfoTape_MarginRight = int(data.appvar.string.(@ID == "UI_InfoTape_MarginRight"));
			
			// Text List *************************
			var textItem:XML;
			for each (textItem in data.textList.text)
			{
				_textData[textItem.@ID] = {value:textItem};
			}
			
			// Menu Init **************************
			CBtnVOList = new Array();
			var level1Item:XML;
			for each (level1Item in data.menu.level1)
			{
				var level1VO:Level1VO = new Level1VO(level1Item);
				if( !Debug && level1VO.ID == "Debug")continue;	// 如果是非Debug状态，则不加载ID为 Debug的项
				CBtnVOList.push(level1VO);
			}
			
			sendLoadedNotification( LOAD_COMPLETE, NAME, SRNAME );
		}
		
		private function errorHandler(e:IOErrorEvent):void 
		{
            sendLoadedNotification( LOAD_ERROR, NAME, SRNAME );
        }
		
		public function GetText(id:String):String{
			
			var text:String = _textData[id]["value"]
			if (text == null){
				trace(("No text for id: " + id));
				return ("");
			};
			return (text);
		}
	}
}